package xyz.jcat.mongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import xyz.jcat.common.enums.IdType;
import xyz.jcat.common.util.IdWorker;
import xyz.jcat.leaf.LeafIdWorker;

@Component
@ConditionalOnProperty(value = "idWorker.mongo.type", havingValue = "leaf")
public class MongoLeafIdWorker<ID extends Number> implements IdWorker {
    @Value("${idWorker.mongo.key}")
    private String key;
    @Value("${idWorker.mongo.idType:long}")
    private String idType;
    @Autowired
    private LeafIdWorker leafIdWorker;

    public ID nextId() {
        Long id = leafIdWorker.nextId(key);
        if(IdType.isInt(idType)) {
            return (ID)Integer.valueOf(id.intValue());
        }
        return (ID)id;
    }

}
